
//
//  FilePathMode.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/17.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "FilePathMode.h"

@implementation FilePathMode
/**
 @brief 数据保存指定路径
 @param modelkey 数据相对路径
 @return 文件保存路径
 */
+ (NSString *)getFilePathWithModelKey:(NSString *)modelkey
{
    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    return [[array objectAtIndex:0] stringByAppendingPathComponent:modelkey];
}

+ (NSString *)getNoteDraftFile {
    return  [self getFilePathWithModelKey:@"NoteDraftMode"];
}

+ (NSString *)getNoteDraftUploadImgFilePath {
    NSString* name_path = [[[self getNoteDraftFile] stringByAppendingPathComponent:@"Draftimages"] stringByAppendingPathComponent:@"uplodaImage"];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:name_path]){
        [fileManager createDirectoryAtPath:name_path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return name_path;
}

+ (NSString *)getNoteDraftDisplayImgFilePath {
    NSString* name_path = [[[self getNoteDraftFile] stringByAppendingPathComponent:@"Draftimages"] stringByAppendingPathComponent:@"displayImage"];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:name_path]){
        [fileManager createDirectoryAtPath:name_path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return name_path;
}

//根据userid生成路径
+ (NSString *)localDocumentPath
{
    //paths： ios下Document路径，Document为ios中可读写的文件夹
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [[paths objectAtIndex:0] stringByAppendingFormat:@"/%@",FileDirectoryName];
    
    return documentDirectory;
}


/*
 * 根据dbName和当前的userid获取数据库路径
 */
+ (NSString *)getDBPath:(NSString *)dbName {
    if ([dbName isEqualToString:IMDatabase]) {
        NSString *documentDirectory = LocalFilePath;
        NSFileManager *manage = [NSFileManager defaultManager];
        [manage createDirectoryAtPath:documentDirectory withIntermediateDirectories:YES attributes:nil error:nil];
        //dbPath： 数据库路径，在Document中。
        NSString *dbPath = [documentDirectory stringByAppendingPathComponent:NSStringFormat(@"%@.db",IMDatabase)];
        
        return dbPath;
    }
    return @"default.db";
}

//根据聊天的对象生成语音的存放路径
+ (NSString *)getChatVoicePath:(NSString *)toFriendid {
    NSString *voiceDir = [NSString stringWithFormat:@"%@/%@/%@", LocalFilePath, MD5Encrypt(objectToString(toFriendid)), @"voice"];
    return voiceDir;
}
//根据聊天的对象生成图片的存放路径
+ (NSString *)getChatImagePath:(NSString *)toFriendid {
    NSString *imageDir = [NSString stringWithFormat:@"%@/%@/%@", LocalFilePath, MD5Encrypt(objectToString(toFriendid)),@"image"];
    return imageDir;
}

+ (BOOL)createFileDirectory:(NSString *)imageDir
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL bol = NO;
    BOOL isDir = NO;
    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        bol = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return bol;
}

+ (BOOL)deleteFileDirectory:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *err;
    [fileManager removeItemAtPath:path error:&err];
    if (err) {
        return NO;
    }
    return YES;
    
}

//录音时生成语音文件名
+ (NSString*)createVoiceFile:(NSString *)friendid {
    NSString* relativePath = [self getRelativePathWithMsgID:nil path:nil messageBodyType:ChatMsgBodyType_Voice];
    
    NSString* realSavePath = LocalVoicePath(friendid, relativePath);
    
    NSString *dir = [realSavePath stringByDeletingLastPathComponent];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:dir]) {
        
        [[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return realSavePath;
}

//选择图片时生成图片文件名
+ (NSString*)creatImageFile:(NSString *)friendid {
    //保存图片
    NSString* relativePath = [FilePathMode getRelativePathWithMsgID:nil path:nil messageBodyType:ChatMsgBodyType_Image];
    NSString* realSavePath = LocalImagePath(friendid, relativePath);
    NSString *dir = [realSavePath stringByDeletingLastPathComponent];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:dir]) {
        
        [[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return realSavePath;
}


+ (NSString *)thumbPath:(NSString *)path
{
    NSString *thumbPath = [NSString stringWithFormat:@"%@_thumb",path];
    return thumbPath;
}

#pragma mark - 转换方法
/**
 *  @brief 获取相对路径
 *  @param storeId  导购Id
 *  @param chatMessage 聊天信息结构体
 */
+(NSString *)getRelativePathWithMsgID:(NSString *)msgID path:(NSString *)path messageBodyType:(ChatMsgBodyType)mType {
    if (msgID.length<1 || path.length<1) {
        
        NSTimeInterval tmpImageID = [[NSDate date] timeIntervalSince1970];
        NSString *tempmsgID = nil;
        if (mType==ChatMsgBodyType_Image) {
            tempmsgID = [NSString stringWithFormat:@"%lld.jpg",(long long)(tmpImageID * 1000)];
        }
        else if (mType==ChatMsgBodyType_Voice)
        {
            tempmsgID = [NSString stringWithFormat:@"%lld.m4a",(long long)(tmpImageID * 1000)];
        }
        else
            tempmsgID = [NSString stringWithFormat:@"%lld",(long long)(tmpImageID * 1000)];
        
        return tempmsgID;
    }
    
    //保存路径
    return path;
}



@end
